package com.deeper.customview.ui.c4.exercise.c04e04;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.deeper.customview.R;

/**
 * 圆形头像，支持动态更新并更新动画
 */
public class CirclePhotoView extends View {
    private static final String TAG = CirclePhotoView.class.getSimpleName();

    private Paint dstPaint;
    private Paint maskPaint;
    private Paint contentPaint;
    private Canvas contentCanvas;
    private Bitmap bmpHeader;
    private Bitmap contentBmp;
    private Bitmap maskBmp;
    private Canvas maskCanvas;
    private Bitmap dstBmp;
    private Canvas dstCanvas;
    private PorterDuffXfermode porterDuffXfermode;
    private int width;
    private int height;
    private Rect srcRec;
    private RectF dstRecF;
    private Bitmap newBitmap;

    public CirclePhotoView(Context context) {
        super(context, null);
    }

    public CirclePhotoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs, 0);
        initPaint();
    }

    public CirclePhotoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initPaint() {
        contentPaint = new Paint();

        maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        maskPaint.setStyle(Paint.Style.FILL);

        dstPaint = new Paint();
        porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);

        bmpHeader = BitmapFactory.decodeResource(getResources(), R.mipmap.dog, null);
        srcRec = new Rect(0, 0, bmpHeader.getWidth(), bmpHeader.getHeight());
        dstRecF = new RectF(0, 0, width, height);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        width = 0;
        height = 0;
        if (w > h) {
            width = height = h;
        } else {
            width = height = w;
        }
        srcRec.set(0, 0, bmpHeader.getWidth(), bmpHeader.getHeight());
        dstRecF.set(0, 0, width, height);

        if (contentBmp == null) {
            contentBmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            contentCanvas = new Canvas(contentBmp);
        }

        if (maskBmp == null) {
            maskBmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            maskCanvas = new Canvas(maskBmp);
        }

        if (dstBmp == null) {
            dstBmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            dstCanvas = new Canvas(dstBmp);
        }
        Log.d(TAG, "onSizeChanged: ");
        if (newBitmap != null) {
            setBitmap(newBitmap);
            Log.d(TAG, "onSizeChanged:  newBitmap");
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(TAG, "onDraw: ");
        contentCanvas.drawBitmap(bmpHeader, srcRec, dstRecF, contentPaint);
        maskCanvas.drawCircle(width / 2, width / 2, width / 2, maskPaint);

        int layer = canvas.saveLayer(dstRecF, null);
        dstCanvas.drawBitmap(contentBmp, 0, 0, null);
        dstPaint.setXfermode(porterDuffXfermode);
        dstCanvas.drawBitmap(maskBmp, 0, 0, dstPaint);
        dstPaint.setXfermode(null);
        canvas.restoreToCount(layer);
        canvas.drawBitmap(dstBmp, 0, 0, dstPaint);
    }

    //互动部分

    /**
     * 设置需要显示的图片。如果为空，则表示恢复默认图
     */
    public void setBitmap(Bitmap bitmap) {
        Log.d(TAG, "setBitmap: ");
        newBitmap = bitmap;
        if (newBitmap == null) {
            bmpHeader = BitmapFactory.decodeResource(getResources(), R.mipmap.dog, null);
        } else {
            bmpHeader = Bitmap.createBitmap(newBitmap);
        }
        srcRec.set(0, 0, bmpHeader.getWidth(), bmpHeader.getHeight());
        dstRecF.set(0, 0, width, height);

        invalidate();
    }
}
